package gongqing.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xkh.xkh.R;

import gongqing.network.http.GetBasicDeviceInfo;
import gongqing.views.adapters.DevTypeChooseAdapter;
import gongqing.views.adapters.DeviceAdapter;
import com.xkh.xkh.MyApplication;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import managers.UIHandler;
import shuiyuqing.network.http.DownloadInfo;

/**
 * 工情基本信息界面
 * Created by 凯 on 2015/11/14.
 */
public class GongqingActivity extends UIHandler.BaseActivity implements GetBasicDeviceInfo.Delegate {

    // 返回
    Button btn_back;

    // 基本设备
    ListView lv_basicDevice;

    // 选择
    Spinner sp_devType;

    Spinner sp_devState;

    // adapter
    DeviceAdapter deviceAdapter;

    int devState = -1;
    int devType = -1;

    PullToRefreshListView pullToRefreshListView;
     //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gongqing);
        initview();
//        initdata();

        showDialog();

    }

    // 初始化数据
    private void initdata() {
        GetBasicDeviceInfo.Upload upload = new GetBasicDeviceInfo.Upload();
        upload.setFlag(1);
        new GetBasicDeviceInfo(this, upload).get();
    }

    View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_back)
            {
                finish();
            }
        }
    };

    // 初始化界面
    private void initview() {
        // 返回
        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(itemClick);

        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.id_refreshlistview);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getmore();
            }
        });
        // 信息列表
        lv_basicDevice = pullToRefreshListView.getRefreshableView();

        sp_devState = (Spinner) findViewById(R.id.id_sp_state);
        sp_devType = (Spinner) findViewById(R.id.id_sp_type);

        DevTypeChooseAdapter devTypeChooseAdapter = new DevTypeChooseAdapter();
        devTypeChooseAdapter.setDevtype(MyApplication.share().getResources().getStringArray(R.array.devType));
        sp_devType.setAdapter(devTypeChooseAdapter);
        sp_devType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                devType = position;
                checkRequest();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        DevTypeChooseAdapter stateAdapter = new DevTypeChooseAdapter();
        stateAdapter.setDevtype(MyApplication.share().getResources().getStringArray(R.array.stateName));
        sp_devState.setAdapter(stateAdapter);
        sp_devState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                devState = position;
                checkRequest();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        // 设置设备数据源
        deviceAdapter = new DeviceAdapter();
        GetBasicDeviceInfo getBasicDeviceInfo = new GetBasicDeviceInfo(null,null);
        deviceAdapter.setGetBasicDeviceInfo(getBasicDeviceInfo);
        lv_basicDevice.setAdapter(deviceAdapter);
        lv_basicDevice.setDividerHeight((int)getResources().getDimension(R.dimen.y8));

    }

    // 加载更多
    private void getmore() {

        String[] devTypeCode = MyApplication.share().getResources().getStringArray(R.array.devTypeCode);
        String[] devStateName = MyApplication.share().getResources().getStringArray(R.array.stateName);
        GetBasicDeviceInfo.Upload upload = new GetBasicDeviceInfo.Upload();
        upload.setDevType(devTypeCode[devType]);
        upload.setDevState(devStateName[devState]);
        upload.setFrom(deviceAdapter.getCount());

        new GetBasicDeviceInfo(GongqingActivity.this, upload).get();
    }


    // 检查请求
    public void checkRequest()
    {
        deviceAdapter.getGetBasicDeviceInfo().getDownload().getDetails().clear();
        deviceAdapter.notifyDataSetChanged();
        if (devState != -1 && devType != -1)
        {
            String[] devTypeCode = MyApplication.share().getResources().getStringArray(R.array.devTypeCode);
            String[] devStateName = MyApplication.share().getResources().getStringArray(R.array.stateName);

            GetBasicDeviceInfo.Upload upload = new GetBasicDeviceInfo.Upload();
            upload.setDevType(devTypeCode[devType]);
            upload.setDevState(devStateName[devState]);

            new GetBasicDeviceInfo(GongqingActivity.this, upload).get();
        }
    }

    // 获取设备信息后
    @Override
    public void onResponse(String response, GetBasicDeviceInfo getBasicDeviceInfo) {
        pullToRefreshListView.onRefreshComplete();
        if (getBasicDeviceInfo.getDownload().getStatus() == DownloadInfo.SUCCESS)
        {
            if (getBasicDeviceInfo.getDownload().getDetails().size() == 0)
            {
                Toast.makeText(MyApplication.share(),"没有更多数据了",Toast.LENGTH_SHORT).show();
            }
            deviceAdapter.getGetBasicDeviceInfo().getDownload().getDetails().addAll(getBasicDeviceInfo.getDownload().getDetails());
            deviceAdapter.notifyDataSetChanged();
        }else if (getBasicDeviceInfo.getDownload().getStatus() == DownloadInfo.FAILED)
        {

        }

        closeDialog();
    }


    @Override
    public void onErrorResponse(VolleyError error, GetBasicDeviceInfo getBasicDeviceInfo) {
        pullToRefreshListView.onRefreshComplete();
        closeDialog();
    }
}
